using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace WhsPhotoUpload.Helpers
{
    public class RegistryTools
    {

        // Yizhe's Version
        public enum WhsPhotoUploadSettings 
        { 
            FlickrApplicationKey, 
            FlickrSharedSecret, 
            PhotoPickupFolder, 
            PhotoFinishedFolder, 
            UploaderRootPath, 
            FlickrToken 
        };

        public static string GetAppSetting(WhsPhotoUploadSettings targetSetting)
        {
            string toReturn = null;
            string appSubKeyPath = @"Software\SingaporeCommunityLabs\WhsPhotoUpload";

            Microsoft.Win32.RegistryKey appSubKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(appSubKeyPath);

            // Check to see if our application subkey exists
            if (appSubKey != null)
            {
                // Yes it does, let's figure out which value name to look for
                string settingName;
                switch (targetSetting)
                {
                    case WhsPhotoUploadSettings.FlickrApplicationKey:
                        settingName = "FlickrApplicationKey";
                        break;
                    case WhsPhotoUploadSettings.FlickrSharedSecret:
                        settingName = "FlickrSharedSecret";
                        break;
                    case WhsPhotoUploadSettings.PhotoPickupFolder:
                        settingName = "PhotoPickupFolder";
                        break;
                    case WhsPhotoUploadSettings.PhotoFinishedFolder:
                        settingName = "PhotoFinishedFolder";
                        break;
                    case WhsPhotoUploadSettings.UploaderRootPath:
                        settingName = "UploaderRootPath";
                        break;
                    case WhsPhotoUploadSettings.FlickrToken:
                        settingName = "FlickrToken";
                        break;
                    default:
                        settingName = "unknown";
                        break;
                }

                // We now access the registry for the value
                if (null != appSubKey.GetValue(settingName))
                    toReturn = appSubKey.GetValue(settingName).ToString();
            }

            return (toReturn);
        }

        public static void SetAppSetting(WhsPhotoUploadSettings targetSetting, string newValue)
        {
            string appSubKeyPath = @"Software\SingaporeCommunityLabs\WhsPhotoUpload";

            Microsoft.Win32.RegistryKey appSubKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(appSubKeyPath, true);

            // Check to see if our application subkey exists.  If not, create it
            if (appSubKey == null)
            {
                // Create the key
                Registry.LocalMachine.CreateSubKey(appSubKeyPath);

                // Reload the key
                appSubKey = Registry.LocalMachine.OpenSubKey(appSubKeyPath, true);

                // Set default values
                appSubKey.SetValue("FlickrApplicationKey", (string)@"");
                appSubKey.SetValue("FlickrSharedSecret", (string)@"");

                appSubKey.SetValue("PhotoPickupFolder", (string)@"");
                appSubKey.SetValue("PhotoFinishedFolder", (string)@"");

                appSubKey.SetValue("UploaderRootPath", (string)@"");
                appSubKey.SetValue("FlickrToken", (string)@"");

            }

            // Let's now set which value we'll be setting
            string settingName;
            switch (targetSetting)
            {
                case WhsPhotoUploadSettings.FlickrApplicationKey:
                    settingName = "FlickrApplicationKey";
                    break;
                case WhsPhotoUploadSettings.FlickrSharedSecret:
                    settingName = "FlickrSharedSecret";
                    break;
                case WhsPhotoUploadSettings.PhotoPickupFolder:
                    settingName = "PhotoPickupFolder";
                    break;
                case WhsPhotoUploadSettings.PhotoFinishedFolder:
                    settingName = "PhotoFinishedFolder";
                    break;
                case WhsPhotoUploadSettings.UploaderRootPath:
                    settingName = "UploaderRootPath";
                    break;
                case WhsPhotoUploadSettings.FlickrToken:
                    settingName = "FlickrToken";
                    break;
                default:
                    settingName = "unknown";
                    break;
            }

            // We now make the setting
            appSubKey.SetValue(settingName, (string)newValue);

        }

        //public enum WHSPhotoUploadSettings { FlickrApplicationKey, FlickrSharedSecret, PhotoPickupFolder, PhotoFinishedFolder, UploaderRootPath };

        //public static string GetAppSetting(WHSPhotoUploadSettings targetSetting)
        //{
        //    string toReturn = null;
        //    string appSubKeyPath = @"Software\SingaporeMIC\WHSPhotoUpload";

        //    Microsoft.Win32.RegistryKey appSubKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(appSubKeyPath);

        //    // Check to see if our application subkey exists
        //    if (appSubKey != null)
        //    {
        //        // Yes it does, let's figure out which value name to look for
        //        string settingName;
        //        switch (targetSetting)
        //        {
        //            case WHSPhotoUploadSettings.FlickrApplicationKey:
        //                settingName = "FlickrApplicationKey";
        //                break;
        //            case WHSPhotoUploadSettings.FlickrSharedSecret:
        //                settingName = "FlickrSharedSecret";
        //                break;
        //            case WHSPhotoUploadSettings.PhotoPickupFolder:
        //                settingName = "PhotoPickupFolder";
        //                break;
        //            case WHSPhotoUploadSettings.PhotoFinishedFolder:
        //                settingName = "PhotoFinishedFolder";
        //                break;
        //            case WHSPhotoUploadSettings.UploaderRootPath:
        //                settingName = "UploaderRootPath";
        //                break;
        //            default:
        //                settingName = "unknown";
        //                break;
        //        }

        //        // We now access the registry for the value
        //        if (null != appSubKey.GetValue(settingName))
        //            toReturn = appSubKey.GetValue(settingName).ToString();
        //    }

        //    return (toReturn);
        //}

        //public static void SetAppSetting(WHSPhotoUploadSettings targetSetting, string newValue)
        //{
        //    string appSubKeyPath = @"Software\SingaporeMIC\WHSPhotoUpload";

        //    Microsoft.Win32.RegistryKey appSubKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(appSubKeyPath, true);

        //    // Check to see if our application subkey exists.  If not, create it
        //    if (appSubKey == null)
        //    {
        //        // Create the key
        //        Registry.LocalMachine.CreateSubKey(appSubKeyPath);

        //        // Reload the key
        //        appSubKey = Registry.LocalMachine.OpenSubKey(appSubKeyPath, true);

        //        // Set default values
        //        appSubKey.SetValue("FlickrApplicationKey", (string)@"");
        //        appSubKey.SetValue("FlickrSharedSecret", (string)@"");

        //        appSubKey.SetValue("PhotoPickupFolder", (string)@"");
        //        appSubKey.SetValue("PhotoFinishedFolder", (string)@"");

        //        appSubKey.SetValue("UploaderRootPath", (string)@"");

        //    }

        //    // Let's now set which value we'll be setting
        //    string settingName;
        //    switch (targetSetting)
        //    {
        //        case WHSPhotoUploadSettings.FlickrApplicationKey:
        //            settingName = "FlickrApplicationKey";
        //            break;
        //        case WHSPhotoUploadSettings.FlickrSharedSecret:
        //            settingName = "FlickrSharedSecret";
        //            break;
        //        case WHSPhotoUploadSettings.PhotoPickupFolder:
        //            settingName = "PhotoPickupFolder";
        //            break;
        //        case WHSPhotoUploadSettings.PhotoFinishedFolder:
        //            settingName = "PhotoFinishedFolder";
        //            break;
        //        case WHSPhotoUploadSettings.UploaderRootPath:
        //            settingName = "UploaderRootPath";
        //            break;
        //        default:
        //            settingName = "unknown";
        //            break;
        //    }

        //    // We now make the setting
        //    appSubKey.SetValue(settingName, (string)newValue);

        //}
    }
}
